package com.example.clientend.job;

import com.example.clientend.domain.ComputerInfo;
import com.example.clientend.utils.InfoGetter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 被控端定时发送状态信息至主控端，以确认被控端是否在线
 */
@Service
@EnableScheduling
@Slf4j
public class SendAliveMsg {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private InfoGetter infoGetter;
    
    @Value("${url.sendAliveMsgUrl}")
    private String sendAliveMsgUrl;

    //每30秒定时发送一次存活信息
    @Scheduled(cron = "*/30 * * * * ?")
    private void sendAliveMsg(){
        // 获取本机状态信息
        ComputerInfo computerInfo = infoGetter.getComputerInfo();
        log.info("尝试发送本机状态信息：" + computerInfo);
        try {
            String result = restTemplate.postForObject(sendAliveMsgUrl, computerInfo, String.class);
            log.info(result);
        } catch (Exception e) {
            log.info("状态信息发送失败！");
        }
    }
    
}
